"""
author: liuxu
date: 2025/3/6
description: 
"""
import allure
import pytest
import glob
import re
from common.logger_ctrl import mylogger
from common.ssh_command import exec_cmd
from base_public.testbase import BasePublic

@pytest.mark.skip(reason="该功能未完成，暂时屏蔽测试")
@pytest.mark.high
@allure.feature('核函数CoreDump分析')
@pytest.mark.skipif("Vega 20" in exec_cmd("hy-smi --showproductname")[1], reason="vega 20环境不支持，跳过此测试用例")
class TestPMC(BasePublic):
    def test_input_core(self, test_env):
        """
        分析核心转储文件的内存快照信息
        :return:
        (base) [root@kme26 _a.out_351719_20250307020810787346]# tree
        .
        ├── agent_4
        ├── agent_5
        ├── agent_6
        ├── codes
        │   ├── code_object_0
        │   ├── code_object_0.asm
        │   ├── code_object_0.info
        │   ├── code_object_1
        │   └── code_object_1.info
        ├── core_0
        ├── environment
        ├── fault_info
        └── queues
            ├── 0
            │   ├── aql_rb
            │   ├── cwsr
            │   ├── packets
            │   └── queue
            └── 1
                ├── aql_rb
                ├── cwsr
                ├── packets
                ├── queue
                └── waves
        """
        # 编译待测用例
        exe_path = self.compile_source_code(src_file_name="core_dump/core_demo.cpp", compile_paras="-O0 -g")

        # 执行用例，生成core文件
        ret0, output0 = exec_cmd(rf"{exe_path}",  cwd=test_env)
        assert ret0 is False
        # 检测是否生成core文件
        core_l = glob.glob(pathname="*core_demo*/core_0", recursive=True)
        assert len(core_l) >= 1, "The core file was not obtained"
        core_name = core_l[0]

        # 使用hipprof分析core文件
        ret, output = exec_cmd(rf"hipprof --input-core {core_name}",  cwd=test_env)
        assert ret is True
        # 判断生成文件
        expect_files_patterns=  ["agent_*", "codes/code_object_*", "environment", "fault_info", "queues/*"]
        for pattern in expect_files_patterns:
            # 根据模式查找匹配的文件或目录
            matches = glob.glob(pattern)
            if matches:
                for match in matches:
                    with open(match, 'r', encoding='utf-8') as file:
                        content = file.read()
                    mylogger.infof(f"cat {matches}\n{content}")
            else:
                pytest.fail(f"The file {pattern} was not obtained")



